package com.cloud.clouduser.security.base.code.imagecode;

import com.cloud.cloudcommons.Redis.SessionConsistance;
import com.cloud.clouduser.security.base.SecurityPorperties;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
import org.springframework.social.connect.web.SessionStrategy;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.imageio.ImageIO;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * @Aurher: QiaoHang
 * @Description:
 * @Data: 2019/9/25 10:08
 * @Modified By:
 */
@RestController
@RequestMapping("/code")
public class ValidateCodeGenerratorController {

    SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();

    @Autowired
    ImageCodeGenerator imageCodeGenerator;

    @RequestMapping(value = "/generator/imageCode.do",method = RequestMethod.GET)
    public void generatorImageCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //创建一个imageCode对象
        ImageCode imageCode = imageCodeGenerator.createImageCode(request);
        //将code放入session/redis....
        sessionStrategy.setAttribute(new ServletWebRequest(request), SessionConsistance.SESSION_IMAGE_CODE,imageCode);
        //通过io流写入浏览器html
        ImageIO.write(imageCode.getBufferedImage(),"JPEG",response.getOutputStream());
    }



}
